<?php
/*
	用户端公共文件，访问用户端时默认加载
*/
defined('BY_JYA') or exit('error');
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$_RQ['op']=$_RQ['op']?$_RQ['op']:'index';
if($_RQ['op']=='index'){
	//还不是分销商，跳转申请页
	$return['apply']=$this->get('agent',array('mid'=>$_SESSION['uid']));
	$return['apply']['area']=json_decode($return['apply']['pcd'],true);
	$return['apply']['codeimg']=json_decode($return['apply']['codeimg'],true);
	$return['become']=$this->setting['become'];
	$return['htmls']=$this->template_url('apply');
	$return['page_setting']=$this->plugin_setting('page');
	$return['apply_setting']=$this->plugin_setting('apply');
	$return['item']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
	$return['item']['mid']=$_SESSION['uid'];
	$return['item']['oldmobile']=$return['item']['mobile'];
	$return['item']=array_merge($return['item'],$return['apply']);
	$setting=uni_setting('register');
	$return['item']['login_type']=$setting['login_type'];
	if($_SESSION['wxapp_id'] && !$_SESSION['fromh5wxapp']){
		$return['areas']=$this->model()->available_areas(1,1);
	}else{
		$return['areas']=$this->model()->available_areas();
	}
	$return['item']['addressinfo']=array('city'=>'');
	$return['agent_level']=$this->setting['level'];
	set_html('title','申请自提点');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if($postdata['login_type']>0 && $post['oldmobile']!=$post['mobile'] && !check_sms_code($post['smscode'])){
		$oldmobile=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'mobile');
		if($post['mobile']!=$oldmobile){
			exi('手机验证码错误！','error');
		}
	}
	$member_update=array(
		'realname'=>trim($post['realname']),
		'mobile'=>trim($post['mobile']),
	);
	model('core/member')->member_update($_SESSION['uid'],$member_update);
	//代理地区整理
	if(!$post['area'] || !$post['area']['province']){
		exi('请选择代理地区！','error');
	}
	$area_code=$this->model()->get_area_code($post['area']);
	$post['address']=$post['addressinfo']['address'];
	$post['lat_lng']=$post['addressinfo']['lat_lng'];
	$post=array(
		'id'=>$post['id'],
		'mid'=>$_SESSION['uid'],
		'title'=>trim($post['title']),
		'name'=>trim($post['realname']),
		'mobile'=>trim($post['mobile']),
		'province'=>trim($post['area']['province']),
		'city'=>trim($post['area']['city']),
		'district'=>trim($post['area']['district']),
		'street'=>trim($post['area']['street']),
		'pcd'=>ijson_encode($post['area']),
		'area_code'=>$area_code,
		'address'=>$post['address'],
		'lat_lng'=>ijson_encode($post['lat_lng']),
		'codeimg'=>ijson_encode($post['codeimg']),
	);
	$this->model('member')->apply_agent($post);
	exi('申请成功！请耐心等待管理员审核！','','/index/');
}
